<?php include_once ('function/check_login_function.php'); onlyValid(); onlyPatient();?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>


<?php include_once('nav.php'); ?>
      
    <div class="container">
    <div class="row">
    <div class="span8">
      <h1>Connections</h1>
					<?php 
					if (isset($_SESSION['message'])) {
						foreach ($_SESSION['message'] as $message) {
							echo '<div class="alert">'.constant($message) . '</div>';
						}
						unset($_SESSION['message']);
					} 
					
					if (isset($_POST['doctorID']) || isset($_SESSION['saved'])) {
						if (isset($_POST['doctorID'])) {
							$doctor_id = $_POST['doctorID'];
						} else if (isset($_SESSION['saved'])) {
							$doctor_id = $_SESSION['saved'];
							unset($_SESSION['saved']);
						}
						
						$doctor = Doctor::findDoctor($doctor_id);
						echo '<div class="well"><table>';
						echo '<tr><td><b>Name:</b></td><td><b>' . $doctor['user_firstname'] . ' ' . $doctor['user_lastname'] . '</b></td></tr>';
						echo '<tr><td>Gender:</td><td>' . $doctor['user_gender'] . '</td></tr>';
						echo '<tr><td>E-mail:</td><td>' . $doctor['user_email'] . '</td></tr>';
						echo '<tr><td>Contact:</td><td>' . $doctor['user_contact'] . '</td></tr>';
						echo '<tr><td>Speciality:</td><td>' . $doctor['doctor_speciality'] . '</td></tr>';
						echo '<tr><td>Registration No.:</td><td>' . $doctor['doctor_regno'] . '</td></tr>';
						
						$hospital_list = Doctor::findHospitalList($doctor['user_id']);
						if ($hospital_list !== null) {
							foreach ($hospital_list as $hospital) {
								echo '<tr><td>Hospital:</td><td>' . $hospital['hospital_name'] . '</td></tr>';
							}
						}
					
						echo '</table> <br/>';
						
						if ($user->checkRequestPending($doctor_id) === 0) {
							echo constant("MSG_DOCTOR_ATTACHED");
					?>
						<form action="function/form_function.php" method="POST">
						<input type="hidden" id="doctorID" name="doctorID" value="<?php echo $doctor_id; ?>"/>
						<input type="hidden" id="from" name="from" value="request"/>
						<input type="submit" name="removeDoctorRequest" value="Remove Doctor" /> <br /> 
						</form>
					<?php } else if ($user->checkRequestPending($doctor_id) === 1) { ?>
					<?php
							echo constant("MSG_REQUEST_PENDING");
					?>
						<form action="function/form_function.php" method="POST">
						<input type="hidden" id="doctorID" name="doctorID" value="<?php echo $doctor_id; ?>"/>
						<input type="hidden" id="from" name="from" value="request"/>
						<input type="submit" name="removeDoctorRequest" value="Remove Request" /> <br /> 
						</form>
					<?php } else { ?>
						<form action="function/form_function.php" method="POST">
						<label for="note"> Note (Optional): </label> </br>
						<textarea rows="4" cols="50" name="note" id="note"></textarea> </br>
						<input type="hidden" id="doctorID" name="doctorID" value="<?php echo $doctor_id; ?>"/>
						<input type="hidden" id="from" name="from" value="request"/>
						<input type="submit" name="sendDoctorRequest" value="Send Request" /> <br /> 
						</form>
						<?php } ?>
					<a href="request.php">Back</a>
					<?php } else { ?>
                        <div class="alert alert-info">
						<h3>List of requests</h3>
						<ol>
						<?php
							$request_list = $user->getDoctorList();
							if ($request_list !== null) {
								foreach ($request_list as $request) {

									echo '<li>';
									echo User::findName($request['doctor_id']);
									if (intval($request['request_pending']) === 0) {
										echo ' (Accepted) ';
									} else {
										echo ' (Pending) ';
									}
									$doctor_id = $request['doctor_id'];
									
									echo '<form action="function/form_function.php" method="POST">';
									echo '<input type="hidden" id="doctorID" name="doctorID" value="' . $doctor_id . '"/>';
									echo '<input type="hidden" id="from" name="from" value="request.php"/>';
									echo '<input type="submit" name="removeDoctorRequest" value="Remove Request" class="btn btn-primary"/> ';
									echo' </form>';
									echo '</li>';

								}
							} else {
								echo constant("MSG_REQUEST_NONE");
							}
						?>
						</ol>
                        </div>
					<?php } ?>
    </div>
    </div>
    </div> <!-- /container -->
  </body>
</html>